package com.giscloud.amp.controller.auth;

import com.alibaba.fastjson.JSON;
import com.giscloud.amp.controller.BaseController;
import com.giscloud.amp.entity.dto.AmpOrgDTO;
import com.giscloud.amp.entity.vo.AmpOrgTreeVO;
import com.giscloud.amp.entity.vo.AmpOrgVO;
import com.giscloud.amp.entity.vo.OrgTreeVO;
import com.giscloud.amp.service.auth.AmpOrgService;
import com.giscloud.commons.model.Result;
import com.giscloud.token.utils.TokenUserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:gxd
 * @Description:
 * @Date: 17:24 2018/2/24
 * @Modified By:
 */
@SuppressWarnings("SpringJavaAutowiringInspection") //加这个注解让IDE 不报: Could not autowire
@RestController
public class AmpOrgController extends BaseController {
    @Autowired
    private AmpOrgService ampOrgRemoteClient;
    @Autowired
    private TokenUserUtil tokenUserUtil;
    @RequestMapping(value="/amp/ampOrg/findAmpOrgList")
    @ResponseBody
    public List<AmpOrgVO> findAmpOrgList(@RequestBody AmpOrgDTO ampOrgDTO) {
        return ampOrgRemoteClient.findAmpOrgList(ampOrgDTO);
    }

    @RequestMapping(value="/amp/ampOrg/findAmpOrgTree")
    @ResponseBody
    public List<AmpOrgTreeVO> findAmpOrgTree(@RequestBody AmpOrgDTO ampOrgDTO) {
        return ampOrgRemoteClient.findAmpOrgTree(ampOrgDTO);
    }

    @RequestMapping(value="/amp/ampOrg/findOrgTree")
    @ResponseBody
    public List<AmpOrgVO> findOrgTree(@RequestBody AmpOrgDTO ampOrgDTO) {
        return ampOrgRemoteClient.findOrgTree(ampOrgDTO);
    }

    @RequestMapping(value="/amp/ampOrg/findOrgByOrgName")
    @ResponseBody
    public List<AmpOrgVO> findOrgByOrgName(@RequestBody AmpOrgDTO ampOrgDTO) {
        return ampOrgRemoteClient.findOrgByOrgName(ampOrgDTO);
    }

    @RequestMapping(value="/amp/ampOrg/findOrgByOrgId")
    @ResponseBody
    public List<AmpOrgVO> findOrgByOrgId(@RequestBody AmpOrgDTO ampOrgDTO) {
        return ampOrgRemoteClient.findOrgByOrgId(ampOrgDTO);
    }

    @RequestMapping("/amp/ampOrg/listOrgTree")
    @ResponseBody
    public List<OrgTreeVO> listOrgTree(@RequestBody AmpOrgDTO ampOrgDTO){
        return ampOrgRemoteClient.listOrgTree(ampOrgDTO);
    }

    @RequestMapping(value="/amp/ampOrg/add",method = RequestMethod.POST)
    @ResponseBody
    public Result add(@RequestBody AmpOrgDTO ampOrgDTO){
        if(StringUtils.isBlank(ampOrgDTO.getOrgName()) || StringUtils.isBlank(ampOrgDTO.getOrgCode())){
            return Result.error("orgName,orgCode不能为空");
        }
        ampOrgDTO.setCreatedStaffId(tokenUserUtil.getUserId());
        return ampOrgRemoteClient.insert(ampOrgDTO);

    }

    @RequestMapping(value="/amp/ampOrg/edit",method = RequestMethod.POST)
    @ResponseBody
    public Result edit(@RequestBody AmpOrgDTO ampOrgDTO){
        if(ampOrgDTO.getOrgId() == null){
            return Result.error("orgId不能为空");
        }
        ampOrgDTO.setCreatedStaffId(tokenUserUtil.getUserId());
        return ampOrgRemoteClient.update(ampOrgDTO);
    }

    @RequestMapping(value="/amp/ampOrg/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestBody AmpOrgDTO ampOrgDTO){
        if(ampOrgDTO.getOrgId() == null ){
            return Result.error("删除失败，id不能为空");
        }
        if(ampOrgRemoteClient.delete(ampOrgDTO.getOrgId()) >= 1){
            return Result.success("","删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

}
